import os
import random
import shutil
# train_path = "/media/liyan/3b75ab20-92d3-4be6-81c2-4e1798e2fe16/我的数据集/GTSRB数据集/train"
#
# val_path = os.path.join(os.sep.join(train_path.split(os.sep)[:-1]),'val')
#
# if not os.path.exists(val_path):
#     os.mkdir(val_path)
#
# for dir in os.listdir(train_path):
#     val_dir_path = os.path.join(val_path,dir)
#     if not os.path.exists(val_dir_path):
#         os.mkdir(val_dir_path)
#
#     dir_path = os.path.join(train_path,dir)
#     file_names = os.listdir(dir_path)
#     val_file_names = random.sample(file_names,len(file_names)//10)
#     val_file_names = [os.path.join(dir_path,i) for i in val_file_names]
#     for selected_file in val_file_names:
#         shutil.move(selected_file,val_dir_path)
import csv
test_csv_path = "E:\Datasets\GTSRB\Test.csv"
test_dir = "E:\Datasets\GTSRB\Test"
saved_dir = "E:\Datasets\GTSRB\新建文件夹"      #保存路径
with open(test_csv_path) as f:
    reader = csv.reader(f)
    lines = list(reader)
    test_length = len(lines)-1      #减1减去第一行的题目
    for line in lines[1:]:
        width = int(line[0])
        height = int(line[1])
        Roi_X1 = int(line[2])
        Roi_Y1 = int(line[3])
        Roi_X2 = int(line[4])
        Roi_Y2 = int(line[5])
        ClassID = line[6]
        img_path = line[7]

        #------创建类名文件夹--------
        class_dir = os.path.join(saved_dir,ClassID)
        if not os.path.exists(class_dir):
            os.mkdir(class_dir)

        #------放入图片-----------
        img_full_path = os.path.join(test_dir,os.path.basename(img_path))
        # renamed_img_full_path = img_full_path.split(".png")[0]+".jpg"
        # os.rename(img_full_path,renamed_img_full_path)
        # shutil.move(renamed_img_full_path,class_dir)
        shutil.copy(img_full_path,class_dir)
